import user from "@/model/user";
import {
  GraphQLObjectType,
  GraphQLString,
  GraphQLSchema,
  GraphQLList,
  GraphQLInt
} from "graphql";

//定义导航Schema类型
const GraphQLNav = new GraphQLObjectType({
  name: "nav",
  fields: {
    title: { type: GraphQLString },
    description: { type: GraphQLString },
    by: { type: GraphQLString },
    url: { type: GraphQLString },
    tags: { type: GraphQLList(GraphQLString) },
    likes: { type: GraphQLInt },
  },
});

//定义根（查询用）
const Root = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    userList: {
      type: GraphQLList(GraphQLNav),
      async resolve() {
        return user.find({});
      },
    },
  },
});
//Mutation数据库删改增用
const MutationRoot = new GraphQLObjectType({
  name: "Mutation",
  fields: {
    action: {
      type: GraphQLNav,
      args: {
        title: { type: GraphQLString },
        description: { type: GraphQLString },
        by: { type: GraphQLString },
        url: { type: GraphQLString },
        tags: { type: GraphQLList(GraphQLString) },
        likes: { type: GraphQLInt },
      },
      async resolve(
        parent: any,
        args: {[key: string]: any}
      ) {
        const cateList = await user.create({
          title: args.title,
          description: args.description,
          by: args.by,
          url: args.url,
          tags: args.tags,
          likes: args.likes,
        });
        return cateList;
      },
    },
  },
});

export default new GraphQLSchema({
  query: Root, //查询用
  mutation: MutationRoot, //增删改用
});
